package com.lzs.collection.vector;

import java.util.Vector;

public class Vector_ {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        //无参构造器
        Vector vector = new Vector();
        for (int i = 0; i < 10; i++) {
            vector.add(i);
        }
        vector.add(100);
        System.out.println(vector);
        /*
        * 源码解读
        * 1、new Vector() 底层
        * public Vector(){
        * this(10)
        * }
        * 2、new Vector(8) 底层
        * public Vector (int initialCapacity){
        * this(initialCapacity,0)
        * }
        * 3、vector.add(i)
        * public synchronized boolean add (E e){
        * modCount++;
        * ensureCapacityHelper(elementCount+1);
        * elementData[elementCount++]=e;
        * return ture;
        * }
        * 4、确定是否需要扩容；条件为：minCapacity-elementData.length>0
        * private void ensureCapacityHelper(int minCapacity){
        * if(minCapacity-elementData.length>0)
        * grow(minCapacity);
        * }
        * 5、若需要的数据容量大小不够用就扩容，扩容的算法：
        * private void grow(int minCapacity){
        * int oldCapacity =elementData.length;
        * //2倍扩容
        * int newCapacity=oldCapacity+(capacityIncrement>0)?capacityIncrement:oldCapacity);
        * if(newCapacity-minCapacity<0){
        * newCapacity=minCapacity;
        * }
        * if(newCapacity-MAX_APPAY_SIZE>0)
        * newCapacity=hugeCapacity(minCapacity);
        * elementData=Arrays.copyOf(elementData,newCapacity);
        *
        * }
        *
        * */
    }
}
